package com.rightsidetech.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.system.dtos.request.store.StoreAddressSaveReq;
import com.rightsidetech.system.dtos.request.store.StoreAddressUpReq;
import com.rightsidetech.system.dtos.response.store.StoreAddressResp;
import com.rightsidetech.system.entity.store.SysStoreAddress;
import com.rightsidetech.system.mapper.SysStoreAddressMapper;
import com.rightsidetech.system.service.SysStoreAddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.Date;
import java.util.List;

@Service("sysStoreAddressService")
public class SysStoreAddressServiceImpl implements SysStoreAddressService {

    @Resource
    private SysStoreAddressMapper sysStoreAddressMapper;

    @Override
    public ResponseData saveAddress(StoreAddressSaveReq req, Long storeId) {
        SysStoreAddress record = new SysStoreAddress();
        record.setStoreInfoId(storeId);
        record.setStoreName(req.getUserName());
        record.setStorePhone(req.getUserPhone());
        record.setProvince(req.getProvice());
        record.setCity(req.getCity());
        record.setArea(req.getArea());
        record.setAddress(req.getAddress());
        record.setDoorplate(req.getAddressDetails());
        record.setLongitude(req.getLongitude());
        record.setLatitude(req.getLatitude());
        record.setCreatedAt(new Date());
        record.setIsDeleted((byte)0);
        SysStoreAddress address = sysStoreAddressMapper.selectBaseBySaveReq(record);
        if(ObjectUtil.isNotEmpty(address)){
            if(address.getStoreInfoId().equals(storeId)){
                return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"地址重复");
            }
        }
        Integer count = sysStoreAddressMapper.selectCountByStoreId(storeId);
        if(count <= 0){
            record.setIsDefault((byte)1);
        }else {
            record.setIsDefault(req.getIsDefault());
        }
        if(record.getIsDefault() == 1){
            sysStoreAddressMapper.updateIsDefault(storeId);
        }
        if (sysStoreAddressMapper.insertSelective(record) <= 0) {
            return ResponseData.fail(CodeIdEnum.FAILED);
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    @Override
    public ResponseData<List<StoreAddressResp>> addressList(Long storeId) {
        List<StoreAddressResp> list = sysStoreAddressMapper.selectList(storeId);
        return ResponseData.success(list);
    }

    @Override
    public ResponseData update(StoreAddressUpReq req,Long storeId){
        SysStoreAddress address = sysStoreAddressMapper.selectByPrimaryKey(req.getId());
        if(ObjectUtil.isEmpty(address)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到需要操作的地址");
        }
        if(!address.getStoreInfoId().equals(storeId)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"该地址不属于此门店");
        }
        SysStoreAddress record = new SysStoreAddress();
        record.setId(address.getId());
        record.setStoreName(req.getUserName());
        record.setStorePhone(req.getUserPhone());
        record.setProvince(req.getProvice());
        record.setCity(req.getCity());
        record.setArea(req.getArea());
        record.setAddress(req.getAddress());
        record.setDoorplate(req.getAddressDetails());
        record.setLongitude(req.getLongitude());
        record.setLatitude(req.getLatitude());
        record.setUpdatedAt(new Date());
        record.setIsDefault(req.getIsDefault());
        SysStoreAddress check = sysStoreAddressMapper.selectBaseBySaveReq(record);
        if(ObjectUtil.isNotEmpty(check)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"存在重复地址");
        }
        if(req.getIsDefault() == 1){
            sysStoreAddressMapper.updateIsDefault(storeId);
        }
        if (sysStoreAddressMapper.updateByPrimaryKeySelective(record) <= 0) {
            return ResponseData.fail(CodeIdEnum.FAILED);
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    @Override
    public SysStoreAddress selectByPrimaryKey(Long id) {
        return sysStoreAddressMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResponseData del(DelReq req,Long storeId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        SysStoreAddress address = sysStoreAddressMapper.selectByPrimaryKey(req.getId());
        if(ObjectUtil.isEmpty(address) || address.getIsDeleted() == 1){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到需要操作的数据");
        }
        if(!storeId.equals(address.getStoreInfoId())){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"此地址不属于此门店，不允许删除");
        }
        address = new SysStoreAddress();
        address.setId(req.getId());
        address.setIsDeleted((byte)1);
        if (sysStoreAddressMapper.updateByPrimaryKeySelective(address) <= 0) {
            return ResponseData.fail(CodeIdEnum.FAILED);
        }
        return ResponseData.success(CodeIdEnum.Success);
    }
}
